<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text-to-Speech Converter</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        textarea {
            width: 100%;
            height: 120px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            font-size: 16px;
            margin-bottom: 15px;
        }
        .controls {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }
        .control-group {
            flex: 1;
            min-width: 200px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        select, input[type="range"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .audio-container {
            margin-top: 20px;
            display: none;
        }
        audio {
            width: 100%;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .spinner {
            border: 4px solid rgba(0, 0, 0, 0.1);
            border-radius: 50%;
            border-top: 4px solid #4CAF50;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .error {
            color: #d32f2f;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Text-to-Speech Converter</h1>

        <textarea id="textInput" placeholder="请输入要转换的文本..."></textarea>

        <div class="controls">
            <div class="control-group">
                <label for="model">模型</label>
                <select id="model">
                    <option value="multi">混合模型</option>
                    <option value="zh">中文模型</option>
                    <option value="en">英文模型</option>
                    <option value="yue">粤语模型</option>
                </select>
            </div>
            <div class="control-group">
                <label for="language">语言</label>
                <select id="language" disabled>
                    <option value="en">英文</option>
                    <option value="zh" selected>中文</option>
                </select>
            </div>

            <div class="control-group">
                <label for="speed">语速: <span id="speedValue">1.0</span>x</label>
                <input type="range" id="speed" min="0.5" max="2" step="0.1" value="1.0">
            </div>
        </div>

        <button id="convertBtn">转换为语音</button>

        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>正在生成音频...</p>
        </div>

        <div class="error" id="error"></div>

        <div class="audio-container" id="audioContainer">
            <h3>生成的音频：</h3>
            <audio id="audioPlayer" controls></audio>
            <p id="audioText"></p>
            <button id="downloadBtn">下载音频</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const textInput = document.getElementById('textInput');
            const languageSelect = document.getElementById('language');
            const modelSelect = document.getElementById('model');
            const speedSlider = document.getElementById('speed');
            const speedValue = document.getElementById('speedValue');
            const convertBtn = document.getElementById('convertBtn');
            const loading = document.getElementById('loading');
            const errorDiv = document.getElementById('error');
            const audioContainer = document.getElementById('audioContainer');
            const audioPlayer = document.getElementById('audioPlayer');
            const audioText = document.getElementById('audioText');
            const downloadBtn = document.getElementById('downloadBtn');

            let currentAudioUrl = '';

            function toggleLanguageSelect() {
                if (modelSelect.value === 'multi') {
                    languageSelect.disabled = false;
                } else {
                    languageSelect.disabled = true;
                    languageSelect.value = 'zh';
                }
            }

            modelSelect.addEventListener('change', toggleLanguageSelect);
            toggleLanguageSelect();

            speedSlider.addEventListener('input', function() {
                speedValue.textContent = this.value;
            });

            convertBtn.addEventListener('click', function() {
                const text = textInput.value.trim();
                if (!text) {
                    showError('请输入要转换的文本');
                    return;
                }

                loading.style.display = 'block';
                errorDiv.style.display = 'none';
                audioContainer.style.display = 'none';
                convertBtn.disabled = true;

                const data = {
                    text: text,
                    language: languageSelect.value,
                    model: modelSelect.value,
                    speed: speedSlider.value
                };

                fetch('/tts', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                })
                .then(response => response.json())
                .then(data => {
                    currentAudioUrl = data.audio_url;
                    audioPlayer.src = currentAudioUrl;
                    audioText.textContent = `"${data.text}"`;
                    audioContainer.style.display = 'block';
                })
                .catch(error => showError(error.error || '生成音频失败'))
                .finally(() => {
                    loading.style.display = 'none';
                    convertBtn.disabled = false;
                });
            });

            downloadBtn.addEventListener('click', function() {
                if (currentAudioUrl) {
                    const extension = currentAudioUrl.split('.').pop();
                    const link = document.createElement('a');
                    link.href = currentAudioUrl;
                    link.download = `tts_${new Date().toISOString().slice(0,10)}.${extension}`;
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            });

            function showError(message) {
                errorDiv.textContent = message;
                errorDiv.style.display = 'block';
            }
        });
    </script>
</body>
</html>
